#include "ScreenTexture.h"
#include "../Utilities.h"


ScreenTexture::ScreenTexture( IDirect3DDevice9* device, int downsampling) : device(device)
{
	device->GetRenderTarget(0, &hostRenderTargetSurface);
	hostRenderTargetSurface->GetDesc(&hostRenderTargetDescription);

	int width = hostRenderTargetDescription.Width / downsampling;
	int height = hostRenderTargetDescription.Height / downsampling;
	D3DFORMAT format = hostRenderTargetDescription.Format;

	device->CreateTexture(width, height, 1, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT,	&texture, NULL);

	dimensions.top = dimensions.left = 0;
	dimensions.right = width;
	dimensions.bottom = height;
}


ScreenTexture::~ScreenTexture()
{
	SafeRelease(texture);
	SafeRelease(hostRenderTargetSurface);
}


void ScreenTexture::UpdateTexture()
{
	IDirect3DSurface9* destination = NULL;
	texture->GetSurfaceLevel(0, &destination);

	D3DSURFACE_DESC hostRenderTargetDescription;
	LPDIRECT3DSURFACE9 hostRenderTargetSurface;
	device->GetRenderTarget(0, &hostRenderTargetSurface);
	hostRenderTargetSurface->GetDesc(&hostRenderTargetDescription);

	device->StretchRect(hostRenderTargetSurface, &dimensions, destination, &dimensions, D3DTEXF_NONE);

	destination->Release();
}
